/**
 * @mbg.generated generator on Thu Feb 23 20:08:38 CST 2023
 */
package com.ybox.system.service.impl;

import com.hankcs.hanlp.dictionary.py.Pinyin;
import com.hankcs.hanlp.dictionary.py.PinyinDictionary;
import com.ybox.system.domain.SysIcon;
import com.ybox.system.domain.vo.IconVo;
import com.ybox.system.mapper.SysIconMapper;
import com.ybox.system.service.SysIconService;
import java.text.Collator;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;

import static java.util.stream.Collectors.toList;

@Service
public class SysIconServiceImpl implements SysIconService {
    private final SysIconMapper sysIconMapper;

    public SysIconServiceImpl(SysIconMapper sysIconMapper) {
        this.sysIconMapper = sysIconMapper;
    }

    /**
     * mark比较器
     */
    public class IconComparator implements Comparator<String> {
        @Override
        public int compare(String o1, String o2) {
            List<String> collect1 = PinyinDictionary.convertToPinyin(o1).stream().map(Pinyin::toString).collect(toList());
            List<String> collect2 = PinyinDictionary.convertToPinyin(o2).stream().map(Pinyin::toString).collect(toList());
            Collator clt = Collator.getInstance();
            return clt.compare(collect1.toString(), collect2.toString());
        }
    }

    /**
     * 获取所有icon
     * @return
     */
    @Override
    public TreeMap<String, List<IconVo>> selectAll(Integer isFree) {
        List<IconVo> iconVos = sysIconMapper.selectAll(isFree);

        // 先分组,并按mark排序
        TreeMap<String, List<IconVo>> map = iconVos.stream()
                .collect(Collectors.groupingBy(IconVo::getMark,() -> new TreeMap<String,List<IconVo>>(new IconComparator()), toList()));

        // 先排序子项
        for (Map.Entry<String, List<IconVo>> nmap : map.entrySet()) {
            nmap.getValue().sort(Comparator.comparing(IconVo::getId));
        }
        return map;
    }

    /**
     * deleteByPrimaryKey
     * @param id id
     * @return int int
     */
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return sysIconMapper.deleteByPrimaryKey(id);
    }

    /**
     * insert
     * @param row row
     * @return int int
     */
    @Override
    public int insert(SysIcon row) {
        return sysIconMapper.insert(row);
    }

    /**
     * insertSelective
     * @param row row
     * @return int int
     */
    @Override
    public int insertSelective(SysIcon row) {
        return sysIconMapper.insertSelective(row);
    }

    /**
     * selectByPrimaryKey
     * @param id id
     * @return SysIcon SysIcon
     */
    @Override
    public SysIcon selectByPrimaryKey(Integer id) {
        return sysIconMapper.selectByPrimaryKey(id);
    }

    /**
     * updateByPrimaryKeySelective
     * @param row row
     * @return int int
     */
    @Override
    public int updateByPrimaryKeySelective(SysIcon row) {
        return sysIconMapper.updateByPrimaryKeySelective(row);
    }

    /**
     * updateByPrimaryKey
     * @param row row
     * @return int int
     */
    @Override
    public int updateByPrimaryKey(SysIcon row) {
        return sysIconMapper.updateByPrimaryKey(row);
    }

    @Override
    public int updateBatch(List<SysIcon> list) {
        return sysIconMapper.updateBatch(list);
    }

    @Override
    public int updateBatchSelective(List<SysIcon> list) {
        return sysIconMapper.updateBatchSelective(list);
    }
}